package com.kiragame.xchat.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.kiragame.xchat.R;
import com.kiragame.xchat.ViewImageActivity;
import com.kiragame.xchat.object.MessageContent;
import com.kiragame.xchat.object.MessageContent.MsgType;
import com.kiragame.xchat.service.ChatService;
import com.kiragame.xchat.utils.Common;
import com.kiragame.xchat.utils.DataManager;
import com.kiragame.xchat.utils.Emoticons;

public class ChatPrivateAdapter extends ArrayAdapter<MessageContent> {

	private static class ViewHolder {
		public final LinearLayout rootView;
		public final LinearLayout llLeftChat;
		public final ImageView ivLeftAvatar;
		public final TextView tvLeftMessage;
		public final RelativeLayout rlLeftImage;
		public final ProgressBar pbLeftLoading;
		public final ImageView ivLeftImage;
		public final LinearLayout llRightChat;
		public final TextView tvRightMessage;
		public final RelativeLayout rlRightImage;
		public final ProgressBar pbRightLoading;
		public final ImageView ivRightImage;
		public final ImageView ivRightAvatar;
	
		private ViewHolder(LinearLayout rootView, LinearLayout llLeftChat, ImageView ivLeftAvatar, TextView tvLeftMessage, RelativeLayout rlLeftImage, ProgressBar pbLeftLoading, ImageView ivLeftImage, LinearLayout llRightChat, TextView tvRightMessage, RelativeLayout rlRightImage, ProgressBar pbRightLoading, ImageView ivRightImage, ImageView ivRightAvatar) {
			this.rootView = rootView;
			this.llLeftChat = llLeftChat;
			this.ivLeftAvatar = ivLeftAvatar;
			this.tvLeftMessage = tvLeftMessage;
			this.rlLeftImage = rlLeftImage;
			this.pbLeftLoading = pbLeftLoading;
			this.ivLeftImage = ivLeftImage;
			this.llRightChat = llRightChat;
			this.tvRightMessage = tvRightMessage;
			this.rlRightImage = rlRightImage;
			this.pbRightLoading = pbRightLoading;
			this.ivRightImage = ivRightImage;
			this.ivRightAvatar = ivRightAvatar;
		}
	
		public static ViewHolder create(LinearLayout rootView) {
			LinearLayout llLeftChat = (LinearLayout)rootView.findViewById( R.id.ll_left_chat );
			ImageView ivLeftAvatar = (ImageView)rootView.findViewById( R.id.iv_left_avatar );
			TextView tvLeftMessage = (TextView)rootView.findViewById( R.id.tv_left_message );
			RelativeLayout rlLeftImage = (RelativeLayout)rootView.findViewById( R.id.rl_left_image );
			ProgressBar pbLeftLoading = (ProgressBar)rootView.findViewById( R.id.pb_left_loading );
			ImageView ivLeftImage = (ImageView)rootView.findViewById( R.id.iv_left_image );
			LinearLayout llRightChat = (LinearLayout)rootView.findViewById( R.id.ll_right_chat );
			TextView tvRightMessage = (TextView)rootView.findViewById( R.id.tv_right_message );
			RelativeLayout rlRightImage = (RelativeLayout)rootView.findViewById( R.id.rl_right_image );
			ProgressBar pbRightLoading = (ProgressBar)rootView.findViewById( R.id.pb_right_loading );
			ImageView ivRightImage = (ImageView)rootView.findViewById( R.id.iv_right_image );
			ImageView ivRightAvatar = (ImageView)rootView.findViewById( R.id.iv_right_avatar );
			return new ViewHolder( rootView, llLeftChat, ivLeftAvatar, tvLeftMessage, rlLeftImage, pbLeftLoading, ivLeftImage, llRightChat, tvRightMessage, rlRightImage, pbRightLoading, ivRightImage, ivRightAvatar );
		}
	}
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder vh;
		if (convertView == null) {
			View view = mInflater.inflate(R.layout.item_lv_chat_private,
					parent, false);
			vh = ViewHolder.create((LinearLayout) view);
			view.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		AQuery aq = new AQuery(convertView);
		MessageContent item = getItem(position);
		Integer userID = item.getFromUserID();
		if (userID.equals(DataManager.getInstance(context).getUserID())) {
			// Right content
			vh.llLeftChat.setVisibility(View.GONE);
			vh.llRightChat.setVisibility(View.VISIBLE);
			setViewContent(item, vh.tvRightMessage, vh.ivRightAvatar,
					vh.ivRightImage,vh.rlRightImage, vh.pbRightLoading, aq);
		} else {
			// Left content
			vh.llRightChat.setVisibility(View.GONE);
			vh.llLeftChat.setVisibility(View.VISIBLE);
			setViewContent(item, vh.tvLeftMessage, vh.ivLeftAvatar,
					vh.ivLeftImage, vh.rlLeftImage, vh.pbLeftLoading, aq);
		}
		// prevUserID = item.getFromUserID();
		return vh.rootView;
	}

	private void setViewContent(final MessageContent item, TextView tvMessage,
			ImageView ivAvatar, ImageView ivImage, RelativeLayout rlImage, ProgressBar pbProgressBar, AQuery aq) {

		if (item.getAvartar() != null) {
			ImageOptions options = new ImageOptions();
			options.round = 5;
			options.fileCache = true;
			options.memCache = true;
			options.targetWidth = 48;
			// options.fallback =
			aq.id(ivAvatar).image(item.getAvartar(), options);
			ivAvatar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, ViewImageActivity.class);
					intent.putExtra(Common.ExtraKey.IMAGE_URL,
							item.getAvartar());
					context.startActivity(intent);
				}
			});
		}else{
			ivAvatar.setImageResource(R.drawable.ic_no_avatar);
		}

		if (item.getMsgType() == MsgType.IMAGE) {
			tvMessage.setVisibility(View.GONE);
			rlImage.setVisibility(View.VISIBLE);
			if (item.getContent() != null) {
				ImageOptions options = new ImageOptions();
				options.round = 5;
				options.fileCache = true;
				options.memCache = true;
				options.targetWidth = 200;
				// options.fallback =
				aq.id(ivImage).progress(pbProgressBar).image(item.getContent(), options);
				ivImage.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(context,
								ViewImageActivity.class);
						intent.putExtra(Common.ExtraKey.IMAGE_URL,
								item.getContent());
						context.startActivity(intent);
					}
				});
			}
		} else {
			tvMessage.setVisibility(View.VISIBLE);
			rlImage.setVisibility(View.GONE);
			tvMessage.setText(Emoticons.getSmiledText(context,
					item.getContent()));
		}
	}

	private LayoutInflater mInflater;
	private Context context;

	// private Integer prevUserID = null;

	// Constructors
	public ChatPrivateAdapter(Context context, List<MessageContent> objects) {
		super(context, 0, objects);
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	public ChatPrivateAdapter(Context context, MessageContent[] objects) {
		super(context, 0, objects);
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

}
